/**
 * SpringMVC restful 风格
 * 用户控制器，控制关于用户的一些业务逻辑操作
 * @author yanwl
 * @date 2015-03-20
 */
package com.maven.demo.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.maven.demo.model.pojo.User;
import com.maven.demo.service.UserService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author yanwl
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	//每页显示的用户数
	private int pageSize = 10;
	//用户列表页码
	private int currPage = 1;
	
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private UserService userService;
	
	/**
	 * @return 用户列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/test",method=RequestMethod.GET)
	public void test()throws Exception{
		/*List<User> users = userService.selectAllUsers();
		for (User user : users) {
			System.out.println(user.getUserName()+"--"+user.getUserEmail());
		}*/
		
		User user = userService.selectUser(3);
		user.setUserName("李四");
		System.out.println(userService.updateUser(user));
	}
	
	/**
	 * @return 企业列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/userList",method=RequestMethod.GET)
	public ModelAndView userList()throws Exception{
		ModelAndView mav = new ModelAndView();
		List<User> users = userService.selectAllUsers();
		mav.addObject("users",users);
		mav.setViewName("test");
		return mav;
	}
	
	/**
	 * Ajax 根据userID删除企业用户
	 * @return 企业用户列表页面
	 * @throws Exception
	 *//*
	@RequestMapping(value="/operate/userAdmin/deleteCompany",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteCompany(HttpServletRequest request,Integer userId)throws Exception{
		
		User user = userService.selectUserInfoByUserId(userId);
		boolean isSuccess = false;
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		List<Order> order = orderService.selectOrdersByMap(map);
		
		if(order.size() != 0){
			map.put("message", "用户有未完成订单，不能删除！！");
		}else{
			isSuccess = userService.deleteUser(user);
			if(isSuccess) {
				map.put("message", "删除成功！！");
			}else {
				map.put("message", "删除失败！！");
			}
		}
		map.put("isSuccess", isSuccess);
		return map;
	}*/
	
}
